package com.ruitu.mall_98_1.product.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.beanu.arad.base.my_base.MyRefreshBaseFragment;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.dao.api.APIRetrofit;
import com.ruitu.mall_98_1.product.activity.ProductDetailActivity;
import com.ruitu.mall_98_1.product.adapter.EvaluateListAdapter;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.PageModel;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.product.Evaluate;
import com.ruitu.router_module.bean.product.ProDetail;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class EvaluateFragment extends MyRefreshBaseFragment implements ProductDetailActivity.OnProEvaluateListener {
    private TextView tv_empty;//没有评价时的空白view

    private List<Evaluate> evaluateList = new ArrayList<>();
    private EvaluateListAdapter adapter;

    private int currPage = 1;
    private int totalPage = 1;
    private ProDetail proDetail;

    public static EvaluateFragment newInstance() {
        Bundle args = new Bundle();
        EvaluateFragment fragment = new EvaluateFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getInflate() {
        return R.layout.fragment_evaluate;
    }

    @Override
    protected ProgressLayout findProgressLayout(View view) {
        return null;
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new EvaluateListAdapter(getActivity());
        return adapter;
    }

    @Override
    protected void initRecyclerView(View v) {
        super.initRecyclerView(v);
        tv_empty = v.findViewById(R.id.tv_empty);
        lRecyclerView.setEmptyView(tv_empty);
    }

    @Override
    protected LRecyclerView getRecyclerView(View view) {
        return view.findViewById(R.id.l_rcv_evaluate);
    }

    @Override
    protected void onItemClick(View v, int pos) {
//        ToastUtils.showShort("pos = " + pos);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((ProductDetailActivity) getActivity()).setOnProEvaluateListener(this);
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        evaluateList.clear();
        reqList();
    }

    private void reqList() {
        APIRetrofit.getDefault().reqEvaluateList(proDetail.getGoods().getGoodId(), currPage, 20)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult<PageModel<Evaluate>>>() {
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        hideProgress();
                    }

                    @Override
                    public void onNext(BaseResult<PageModel<Evaluate>> pageResult) {
                        hideProgress();
                        if (pageResult.isSuccess()) {
                            currPage = pageResult.getData().getCurrentPage();
                            totalPage = pageResult.getData().getTotalPage();
                            List<Evaluate> newDate = pageResult.getData().getDataList();
                            evaluateList.addAll(newDate);
                            adapter.setDataList(evaluateList);
                        } else {
                            ToastUtils.showShort(pageResult.getMsg());
                        }
                    }
                });
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqList();
        } else {
            refreshNoMoreData();
        }
    }

    @Override
    public void onReqEvalute(ProDetail proDetail) {
        this.proDetail = proDetail;
        reqFirst();
    }

    @Override
    public void hideProgress() {
        super.hideProgress();
        refreshComplete();
    }

    protected void reReq() {
    }
}
